package com.yzd.netmonitor.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.yzd.netmonitor.exception.ResponseCodeServiceException;
import com.yzd.netmonitor.web.RespBody;
import com.yzd.netmonitor.web.RespBodyBuilder;
import com.yzd.netmonitor.web.ResponseCode;

public class BaseController {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	protected RespBodyBuilder respWriter = new RespBodyBuilder();

	@ExceptionHandler(value = Exception.class)
	public RespBody handleException(Exception e) {

		if (e instanceof ResponseCodeServiceException) {
			ResponseCodeServiceException rcse = (ResponseCodeServiceException) e;
			return respWriter.toError(rcse.getResponseCode());
		}
		logger.error(e.getMessage(), e);
		return respWriter.toError(ResponseCode.CODE_500);
	}
}
